package com.winsomely.leecode;

/**
 * 最长回文串
 * 
 * @author liujiawei
 *
 */
public class LongestPalindrome2 {

	public int longestPalindrome(String s) {
		int[] data = new int[52];
		for (int i = 0; i < s.length(); i++) {
			char c = s.charAt(i);
			if (c < 97) {
				data[c - 'A']++;
			} else {
				data[c - 'a' + 26]++;
			}
		}
		int result = 0;
		for (int i = 0; i < data.length; i++) {
			if (data[i] > 0) {
				result += data[i] % 2 == 0 ? data[i] : data[i] - 1;
			}
		}
		return result == s.length() ? result : result + 1;
	}
	
	public int longestPalindrome1(String s) {
		int[] data = new int[52];
		for (int i = 0; i < s.length(); i++) {
			char c = s.charAt(i);
			if (c < 97) {
				data[c - 'A']++;
			} else {
				data[c - 'a' + 26]++;
			}
		}
		int result = 0;
		for (int i = 0; i < data.length; i++) {
			if (data[i] > 0) {
				result += data[i] % 2 == 0 ? data[i] : data[i] - 1;
			}
		}
		return result == s.length() ? result : result + 1;
	}

	public static void main(String[] args) {
		LongestPalindrome2 t = new LongestPalindrome2();
		String s = "abcccbbbb";
		System.out.println(t.longestPalindrome1(s));
	}

}
